﻿var map,defaultSymbol,highlightSymbol,resultTemplate,drawToolbar;
require([
    "esri/map",
    "esri/layers/FeatureLayer",
    "esri/toolbars/draw",
    "esri/tasks/QueryTask",
    "esri/tasks/query",
    "esri/symbols/SimpleMarkerSymbol",
    "esri/symbols/SimpleLineSymbol",
    "esri/Color",
    "esri/InfoTemplate",

    "dojo/domReady!"
], function(
    Map,
    FeatureLayer,
    Draw,
    QueryTask,
    Query,
    SimpleMarkerSymbol,
    SimpleLineSymbol,
    Color,
    InfoTemplate

){
    map = new Map("map", {
        basemap: "streets",
        center: [120.148,30.158],
        zoom: 14,
        slider: false,
        showInfoWindowOnClick:false,
        displayGraphicsOnPan:false
    });
    drawToolbar = new Draw(map);
    //点默认显示样式
    defaultSymbol = new SimpleMarkerSymbol(
        SimpleMarkerSymbol.STYLE_DIAMOND, 15,
        new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
            new Color([0,0,0]), 1),
        new Color([255,255,0,0.5]));
    //点高亮显示样式
    highlightSymbol = new SimpleMarkerSymbol(
        SimpleMarkerSymbol.STYLE_DIAMOND, 15,
        new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
            new Color([0,0,0]), 1),
        new Color([255,0,0,0.5]));

    var queryTask = new QueryTask("http://192.168.1.157:8399/arcgis/rest/services/zhxtd/ground_plan/MapServer/0")
    var query = new Query();
    query.returnGeometry = true;
    query.outFields = ["STNM","内底标高","最大深度_m","地面标高"];
    //query.outFields =["*"];
        queryTask.execute(query,addPointsToMap);
    resultTemplate = new InfoTemplate("FID","<tr><td>${STNM}</td><td>${内底标高}</td><td>${最大深度_m}</td><td>${地面标高}</td></tr>");
    function addPointsToMap(featureSet)
    {
        var features = featureSet.features;
        //遍历查询结果把结果以点默认显示样式添加到map的GraphicsLayer中进行显示
        for (var i=0, il=features.length; i<il; i++)
        {
            map.graphics.add(features[i].setSymbol(defaultSymbol).setInfoTemplate(resultTemplate));
        }

    }
    function doSearch(ele)
    {
        if(ele.value=="进行框选查询")
        {
            ele.value="取消框选查询";
            //添加onDrawEnd完成后监听事件调用findPointsInExtent方法
            dojo.connect(drawToolbar, "onDrawEnd", findPointsInExtent);
            drawToolbar.activate(Draw.EXTENT);
        }
        else
        {
            ele.value="进行框选查询";
            drawToolbar.deactivate();
        }

    }

    function findPointsInExtent(extent)
    {
        var results = [];
        var graphics = map.graphics.graphics;
        //对map的GraphicsLayer的元素进行遍历把包含在extent元素进行高亮显示,同时把元素的属性值放到results数组中
        for(var i=0;i<graphics.length;i++)
        {
            var graphic=graphics[i];
            if(extent.contains(graphic.geometry))
            {
                graphic.setSymbol(highlightSymbol);
                //getContent()返回以InfoTemplate为模板的属性值
                results.push(graphic.getContent());
            }
            else
            {
                graphic.setSymbol(defaultSymbol);
            }
        }
        //显示查询到的结果个数
        dojo.byId("inextent").innerHTML = "结果数量："+results.length+"个";
        //输出查询到的结果列表
        dojo.byId("results").innerHTML = "<table border='1px'><tr><td>AREANAME</td><td>ST</td><td>CAPITAL</td><td>POP2000</td></tr>" + results.join("") + "</table>";
    }
});